import { _decorator, Component, instantiate, Node, Vec3 } from 'cc';
import { GAME_PREFAB } from '../../Data/GlobalData';
import { BaseComponent } from '../../Manager/UIManager/BaseComponent';
import { Coin } from './Coin';
import { TweenTools } from '../../Tool/TweenTools';
const { ccclass, property } = _decorator;

@ccclass('CoinLoad')
/**
 * 金币层
 */
export class CoinLoad extends BaseComponent {

    //金币数组
    private CoinArray: Node[] = [];

    //没有用得金币
    private NouseCoin: Node[] = [];


    //记录出现金币得次数
    private CoinCount: number = 0;
    private FixCoinCount: number = 0;

    protected OnShow(): void {
        this.CoinCount = 1 + Math.floor(Math.random() * 5);
        this.FixCoinCount = 0;

        for (let coin of this.CoinArray) {
            coin.setPosition(new Vec3(-1000, -1000))
            this.NouseCoin.push(coin);
        };
        this.CoinArray = [];

    }

    /**
     * 添加金币
     */
    public AddCoin(_pos: Vec3) {
        if (this.NouseCoin.length > 0) {
            let coin: Node = this.NouseCoin.shift();
            coin.setPosition(_pos);
            // TweenTools.ScaleBigNoSmall(coin,20);
            this.CoinArray.push(coin);
            return;
        }
        let coin: Node = instantiate(app.resManager.Prefabs[GAME_PREFAB.Coin]);
        this.node.addChild(coin);
        // TweenTools.ScaleBigNoSmall(coin, 20);
        coin.setPosition(_pos);
        this.CoinArray.push(coin);
    }

    /**
     * 添加金币样式
     */
    public AddCoinStyle() {
        if (this.CoinArray.length > 0) return;

        let hor_num: number = 1 + Math.random() * 3;
        let ver_num: number = 1 + Math.random() * 5;
        let disn_num: number = 75;

        for (let i = 0; i < hor_num; i++) {
            for (let j = 0; j < ver_num; j++) {
                this.AddCoin(new Vec3(-disn_num * (hor_num / 2) + i * disn_num, -disn_num * (ver_num / 2) + j * disn_num, 0));
            }
        }

        // let ran_num: number = Math.floor(Math.random() * 3);
        // switch (ran_num) {
        //     case 0:
        //         break;
        //     case 1:
        //         break;
        //     case 2:
        //         break;
        // }
    }

    /**
     * 碰到了金币
     */
    public HitCoin(_coin: Node) {
        _coin.setPosition(new Vec3(1000, 1000))
        let index = this.CoinArray.indexOf(_coin);
        if (index != -1) {
            this.CoinArray.splice(index, 1);
            this.NouseCoin.push(_coin);
        }
    }

    /**
     * 添加金币
     */
    public AddCoinCount() {
        this.FixCoinCount++;
        if (this.FixCoinCount == this.CoinCount) {
            this.CoinCount = 1 + Math.floor(Math.random() * 5);
            this.FixCoinCount = 0;
            this.AddCoinStyle();
        }
    }

}


